import Axios from 'axios'
import Cookies from 'js-cookie';
import {Message, Spin} from 'iview';
import router from '../router'


Axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
Axios.defaults.headers.put['Content-Type'] = 'application/json; charset=utf-8';
Axios.defaults.timeout = 100000;


//本地测试
// Axios.defaults.baseURL = 'http://127.0.0.1:8080';


//测试服务器
//Axios.defaults.baseURL = 'http://kid-debug.color-sz.com/debug';
//Axios.defaults.baseURL = 'http://sa.wekids.group/pro';

//正式服务器
Axios.defaults.baseURL = 'http://kid.color-sz.com/pro';
/**
 *
 * 拦截器
 */
Axios.interceptors.request.use(
  config => {
    config.headers = {
      Authorization: "Bearer " + Cookies.getJSON('TOKEN'),
    };
    return config;
  }
);
Axios.interceptors.response.use(
  response => {
    if (!response.data.code) {
      return response;
    }
    if (response.data.code === 200) {
      return response;
    } else if (response.data.code === 1000 || response.data.code === 401) {
      router.push({
        path: '/login',
        query: {redirect: router.currentRoute.fullPath}
      });
    } else {

      Spin.hide();
      Message.error({
        duration: 3,
        content: response.data.msg,
      });
    }
    // return Promise.reject(response);
  },
)

const fetch = (url, params = {}) => {
  return Axios.get(url, {
    params: params,
  }).then(response => {
    return response.data;
  })
};
const exportExcel = (url, params = {}) => {
  return Axios.get(url, {
    params: params,
    responseType: 'blob'
  }).then(response => {
    let fileName = response.headers['content-disposition'].split("filename=")[1];
    let blob = new Blob([response.data], {
      type: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    })
    if (window.navigator.msSaveOrOpenBlob) {
      navigator.msSaveBlob(blob);
    } else {
      let elink = document.createElement('a');
      elink.download = fileName;
      elink.style.display = 'none';
      elink.href = URL.createObjectURL(blob);
      document.body.appendChild(elink);
      elink.click();
      document.body.removeChild(elink);
    }
  }).catch(error => {
    return Promise.reject(error.data || error);
  });
};

const post = (url, params = {}) => {
  return Axios.post(url, params).then(response => {
    if (response.data && response.data.code === 200) {
      Message.success(response.data.msg);
      return response.data;
    }
    throw response;
  })
};


const put = (url, params = {}) => {
  return Axios.put(url, params).then(response => {
    if (response.data && response.data.code === 200) {
      Message.success(response.data.msg);
      return response.data;
    }
    throw response;
  })
};


const del = (url, params = {}) => {
  return Axios.delete(url, {
    params: params
  }).then(response => {
    if (response.data && response.data.code === 200) {
      Message.success(response.data.msg);
      return response.data;
    }
  })
};

export default {
  del,
  login: post.bind(null, '/login'),
  //------------左侧菜单------------------------
  fetchLeftMenu: fetch.bind(null, '/menu/left-menu'),

  //------------用户------------------------
  fetchUsers: fetch.bind(null, '/user'),
  updateUser: (data) => put.bind(null)('/user', data),// 修改
  createUser: post.bind(null, '/user'),
  delUser: del.bind(null, '/user'),
  resetPwd: (id) => put.bind(null)('/user/resetPwd/' + id),// 重置密码
  fetchCurrentUserInfo: fetch.bind(null, '/user/current'), //当前用户名
  updatePwd: (data) => put.bind(null)('/user/pwd', data),//更新密码
  insertUserRole: (data) => put.bind(null)('/user/role', data),
  lockUser: data => put.bind(null)('/user/lock', data),
  unLockUser: data => put.bind(null)('/user/un-lock', data),

//------------角色------------------------
  fetchRole: fetch.bind(null, '/role'),
  fetchUserRole: fetch.bind(null, '/role/user'),
  createRole: post.bind(null, '/role'),
  updateRole: (data) => put.bind(null)('/role', data),
  delRole: del.bind(null, 'role'),
  updateRoleResource: (data) => put.bind(null)('/role/menu', data),

  //------------菜单------------------------
  fetchMenu: fetch.bind(null, '/menu'),
  fetchMenuSys: fetch.bind(null, '/menu/sys'),
  createMenu: post.bind(null, '/menu'),
  updateMenu: (data) => put.bind(null)('/menu', data),
  delMenu: del.bind(null, 'menu'),
  fetchResource: fetch.bind(null, '/menu/role'),
  flushPermission: fetch.bind(null, '/permission/flush'),
  getLogLog: fetch.bind(null, 'log/login'),
  getOperationLog: fetch.bind(null, 'log/operation'),
  //七牛
  deleteFileFromQiNiu: del.bind(null, 'qiniu/delete-file'),
  deleteFromTencent: del.bind(null, '/cos/del'),

  //区域查询
  fetchArea: (id) => fetch.bind(null)('/area/' + id),
  //渠道、集团
  fetchPlatformStatics: fetch.bind(null, '/merchant/statics/platform'),
  fetchMerchant: fetch.bind(null, '/merchant'),
  createMerchant: post.bind(null, '/merchant'),
  updateMerchant: (data) => put.bind(null)('/merchant', data),
  deleteMerchant: (id) => del.bind(null)('/merchant/' + id),
  //获取平台下的渠道
  fetchOwnChannel: fetch.bind(null, '/merchant/channel'),
  fetchClique: fetch.bind(null, '/merchant/clique'),

  //幼儿园
  fetchKindergarten: fetch.bind(null, '/kindergarten'),
  fetchKindergartenType: fetch.bind(null, '/kindergarten/type'),
  createKindergarten: post.bind(null, '/kindergarten'),
  updateKindergarten: (data) => put.bind(null)('/kindergarten', data),
  kresetPwd:post.bind(null,'/kindergarten/resetpwd'),
  deleteKindergarten: (id) => del.bind(null)('/kindergarten/' + id),
  deleteKindergartenLogo: del.bind(null, '/kindergarten/logo'),
  updateAdminAccount: (data) => put.bind(null)('/kindergarten/account', data),
  updateMerchantAccount: (data) => put.bind(null)('/merchant/account', data),
  fetchKindergartenBriefAndImage: (id) => fetch.bind(null)('kindergarten//brief/image/' + id),
  fetchKindergartenList: fetch.bind(null, '/kindergarten/list'),
  fetchIndexStatics: fetch.bind(null, '/kindergarten/statics'),
  //获取微信公众号二维码
  fetchRegisterCode: fetch.bind(null, '/kindergarten/register/code'),

  //微信配置
  fetchMerchantConfig: (merchantId) => fetch.bind(null)('/wechat/merchant/' + merchantId),
  fetchSchoolWechatConfig: (id) => fetch.bind(null)('/wechat/kindergarten/' + id),
  setMerchantConfig: post.bind(null, '/wechat'),
  //学龄段
  fetchGrade: fetch.bind(null, '/grade'),
  fetchGradeList: fetch.bind(null, '/grade/list'),
  createGrade: post.bind(null, '/grade'),
  updateGrade: (data) => put.bind(null)('/grade', data),
  deleteGrade: del.bind(null, '/grade'),

  //教师
  exportTeacher: exportExcel.bind(null, '/teacher/export'),
  fetchTeacher: fetch.bind(null, '/teacher'),
  createTeacher: post.bind(null, '/teacher'),
  updateTeacher: (data) => put.bind(null)('/teacher', data),
  deleteTeacher: del.bind(null, '/teacher'),
  fetchTeacherList: fetch.bind(null, '/teacher/list'),
  changePermission: (data) => put.bind(null)('/teacher/permission', data),
  //学生
  fetchStudent: fetch.bind(null, '/student'),
  fetchStudentById: (id) => fetch.bind(null, '/student/' + id),
  createStudent: post.bind(null, '/student'),
  // importdata: (data) => post.bind(null, '/'+data+'/importdata'),
  importexcel:(data)=>post.bind(null)('/'+data+'/importdata'),
  updateStudent: (data) => put.bind(null)('/student', data),
  deleteStudent: del.bind(null, '/student'),
  fetchStudentKinsfolk: fetch.bind(null, '/kinsfolk/list/student'),
  updateKinsfolk: (data) => put.bind(null)('/kinsfolk', data),
  createKinsfolk: post.bind(null, '/kinsfolk'),
  deleteKinsfolk: del.bind(null, '/kinsfolk'),
  //班级
  fetchClazz: fetch.bind(null, '/clazz'),
  createClazz: post.bind(null, '/clazz'),
  updateClazz: (data) => put.bind(null)('/clazz', data),
  deleteClazz: del.bind(null, '/clazz'),
  fetchKindergartenClazz: fetch.bind(null, '/clazz/list'),
  //获取年纪和教师
  fetchGradeAndTeacher: fetch.bind(null, 'clazz/grade-teacher'),

  //报告管理
  deleteReport: del.bind(null, '/report'),
  fetchReport: fetch.bind(null, '/report'),
  exportReport: exportExcel.bind(null, '/report/export'),
  exportReport1: exportExcel.bind(null, '/report/export1'),
  exportReport2: exportExcel.bind(null, '/report/export2'),
  exportReport3: exportExcel.bind(null, '/report/export3'),
  exportReport4: exportExcel.bind(null, '/report/export4'),
  exportReport5: exportExcel.bind(null, '/report/export5'),

  //晨检机设备
  fetchDevice: fetch.bind(null, '/device'),
  createDevice: post.bind(null, '/device'),
  updateDevice: (data) => put.bind(null)('/device', data),
  unbindDevice: (id) => put.bind(null)('/device/unbind/' + id),
  changeAttendanceStatus: (data) => put.bind(null)('/device/attendance', data),
  updateDeviceApk: (data) => put.bind(null)('/device/apk', data),
  haveDevice: (data) => post.bind(null)('/device/have', data),

  fetchTerminal: fetch.bind(null, '/atd-terminal'),
  createTerminal: post.bind(null, '/atd-terminal'),
  updateTerminal: (data) => put.bind(null)('/atd-terminal', data),
  unbindTerminal: (id) => del.bind(null)('/atd-terminal/' + id),
  //考勤设置
  fetchAtd: fetch.bind(null, '/attendance-main'),
  createAtd: post.bind(null, '/attendance-main'),
  updateAtd: (data) => put.bind(null)('/attendance-main', data),
  deleteAtd: id => del.bind(null)('/attendance-main/' + id),
  startOrStopAtd: (data) => put.bind(null)('/attendance-main/status', data),
  deleteConfig: id => del.bind(null)('/attendance-config/' + id),

  //学生请假查询
  fetchStuLeave: fetch.bind(null, '/student-leave'),
  fetchStuLeaveexport:  exportExcel.bind(null, '/student-leave/export'),

  //家长反馈
  fetchFeedback: fetch.bind(null, 'feedback'),
  readFeedback: fetch.bind(null, '/feedback/read'),

  //接送记录
  fetchPickDeliver: fetch.bind(null, 'pick-deliver'),
  fetchPickByStuIdAndDay: fetch.bind(null, '/pick-deliver/today'),
  fetchStudentRecord: fetch.bind(null, '/student-record'),
  fetchStudentRecordexport: exportExcel.bind(null, '/student-record/export'),
  exportStudentRecord: fetch.bind(null, '/student-record/export'),

  //教师请假审核
  fetchTeacherLeave: fetch.bind(null, 'teacher-leave'),
  fetchTeacherLeaveexport: exportExcel.bind(null, '/teacher-leave/export'),
  judgeTeacherLeave: (data) => put.bind(null)('teacher-leave/judge', data),
  //通知
  fetchNotice: fetch.bind(null, '/notice'),
  createNotice: post.bind(null, '/notice'),
  deleteNotice: del.bind(null, '/notice'),
  fetchNoticeFeed: fetch.bind(null, '/notice-feedback'),
  //关注微信号的家长
  fetchKinsfolkList: fetch.bind(null, '/kinsfolk/list'),
  //离园申请
  fetchOutApply: fetch.bind(null, '/out-apply'),
  //家长审核
  fetchKinsfolkJudge: fetch.bind(null, '/kinsfolk-judge'),
  killJudge: (data) => put.bind(null)('/kinsfolk-judge/kill', data),
  passJudge: data => put.bind(null)('/kinsfolk-judge/pass', data),
  changeClazzFn: post.bind(null, '/kinsfolk-judge/clazz/change'),

  fetchStuConfig: fetch.bind(null, '/attendance-config/stu'),
  createStuConfig: post.bind(null, '/attendance-config/stu'),
  updateStuConfig: data => put.bind(null)('/attendance-config/stu', data),
  deleteStuConfig: id => del.bind(null)('/attendance-config/' + id),
  fetchTeaAtd: fetch.bind(null, '/attendance-record'),
  exportAtd: exportExcel.bind(null, '/attendance-record/export'),
  fetchGateRecord: fetch.bind(null, '/gate-record'),
  exportGateRecord: exportExcel.bind(null, '/gate-record/export'),

  fetchFaceRecord: fetch.bind(null, '/face-record'),
  judgeFaceRecord: data => put.bind(null)('/face-record/judge', data),
  fetchFaceRecordById: id => fetch.bind(null)('/face-record/' + id),

  fetchCamera: fetch.bind(null, '/camera'),
  createCamera: post.bind(null, '/camera'),
  updateCamera: (data) => put.bind(null)('/camera', data),
  deleteCamera: del.bind(null, '/camera'),

  //收费
  fetchProduct: fetch.bind(null, '/product'),
  createProduct: post.bind(null, '/product'),
  updateProduct: (data) => put.bind(null)('/product', data),
  deleteProduct: del.bind(null, '/product'),

  //apk
  fetchApk: fetch.bind(null, '/apk'),
  createApk: post.bind(null, '/apk'),
  updateApk: (data) => put.bind(null)('/apk', data),
  deleteApk: del.bind(null, '/apk'),
  changeStatus: (data) => put.bind(null)('/apk/status', data),

  //缴费记录
  fetchOrder: fetch.bind(null, '/order'),
  exportOrder: exportExcel.bind(null, '/order/export'),
  //学期
  fetchTerm: fetch.bind(null, '/term'),
  createTerm: post.bind(null, '/term'),
  updateTerm: (data) => put.bind(null)('/term', data),
  deleteTerm: del.bind(null, '/term'),

//显示屏
  fetchShowTerminal: fetch.bind(null, '/show-terminal'),
  updateShowTerminal: (data) => put.bind(null)('/show-terminal', data),
  deleteShowTerminal: del.bind(null, '/show-terminal'),
  unbindShowTerminal: (data) => put.bind(null)('/show-terminal/unbind', data),

  //gps
  fetchGpsList: fetch.bind(null, '/gps/list'),
  bindGPS: post.bind(null, '/student/gps'),

  fetchFood: fetch.bind(null, '/food'),
  createFood: post.bind(null, '/food'),
  updateFood: (data) => put.bind(null)('/food', data),
  deleteFood: del.bind(null, '/food'),

  fetchCookbook: fetch.bind(null, '/cookbook/week'),
  createCookbook: post.bind(null, '/cookbook'),
  deleteCookbook: del.bind(null, '/cookbook'),

  //身高记录
  deleteHeightRecord: del.bind(null, '/height-record'),
  fetchHeightRecord: fetch.bind(null, '/height-record'),
  updateHeightRecord: (data) => put.bind(null)('/height-record', data),
  createHeightRecord: post.bind(null, '/height-record'),
  exportHeightRecord: exportExcel.bind(null, '/height-record/export'),

  //身高记录详情
  deleteHeightRecordDetail: del.bind(null, '/height-record-detail'),
  fetchHeightRecordDetail: fetch.bind(null, '/height-record-detail'),
  updateHeightRecordDetail: (data) => put.bind(null)('/height-record-detail', data),
  createHeightRecordDetail: post.bind(null, '/height-record-detail'),
  exportHeightRecordDetail: exportExcel.bind(null, '/height-record-detail/export'),
  fetchStudentByClazzId: (id) => fetch.bind(null)('/student/studentListByClazzId/' + id),
  fetchHeightRecordDetailByHeightRecordId: (id) => fetch.bind(null)('/height-record-detail/list/' + id),
}
